package com.common.uilt;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;

public class CalculationTool {
	/**
	 * 计算月提折旧
	 * @return
	 */
	public static Double getMD(Double jc,Integer year,Double unit_price){
		try {
			Double jtD = (unit_price - unit_price * (jc / 100)) / (year * 12);
			BigDecimal bg = new BigDecimal(jtD);
			double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			return f1;
		} catch (Exception e) {
			// TODO: handle exception
			return 0.00d;
		}
	}
	/**
	 * 将Double 四色五入转换成保留2位的String
	 * @param str
	 * @return
	 */
	public static String changeString(Double d){
		try {
			
			java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
			String str = df.format(d);
			return str;
		} catch (Exception e) {
			return "0.00";
		}
	}
	public static String changeString(Integer i){
		return changeString(new Double(i));
	}
	public static String changeString(Float f){
		return changeString(new Double(f));
	}
	public static String changeString(String str){
		try {
			return changeString(new Double(str));
			
		} catch (Exception e) {
			// TODO: handle exception
			return "0.00";
		}
	}
	public static String changeString(Long str){
		return changeString(new Double(str));
	}
	public static String changeString(int i){
		return changeString(new Double(i));
	}
	public static String changeString(float i){
		return changeString(new Double(i));
	}
	public static String changeString(long i){
		return changeString(new Double(i));
	}
	public static Double getJc(Double price,Double rate){
		return price*rate/100;
	}
	public static void main(String[] args) {
		System.out.println(changeString(3d));
	}
	/**
	 * 复制一个对象
	 * @param object
	 * @return
	 * @throws Exception
	 */
	public static Object copy(Object object) throws Exception {
		Class<?> classType = object.getClass();
		Object objectCopy = classType.getConstructor(new Class[] {})
				.newInstance(new Object[] {});
		Field fields[] = classType.getDeclaredFields();
		for (int i = 1; i < fields.length; i++) {
			Field field =fields[i];
			String fieldName = getMethodName(field.getName());
			String getMethodName = "get" + fieldName;
			String setMethodName = "set" + fieldName;
			Method getMethod = classType.getMethod(getMethodName,
					new Class[] {});
			Method setMethod = classType.getMethod(setMethodName,
					new Class[] { field.getType() });
			Object value = getMethod.invoke(object, new Object[] {});
			setMethod.invoke(objectCopy, new Object[] { value });
		}
		return objectCopy;
	}
	public static String getMethodName(String fildeName){
		byte[] items = fildeName.getBytes();
		items[0] = (byte) ((char) items[0] - 'a' + 'A');
		return new String(items);
	}
}
